import { Component } from 'vue-facing-decorator'
import toNative from '@/utils/toNative'
import TableRender, { type TableRenderColumnTypes } from '@/render/TableRender'
import type { AnyObject } from '@/types'
import DrawerAdd from './components/DrawerAdd'
import { Button, message } from 'ant-design-vue'
import { PlusOutlined } from '@ant-design/icons-vue'
import { h } from 'vue'
import { deleteConfig, getEmailConfigList } from '@/api/emailConfig'
// import DrawerPreview from './components/DrawerPreview'
import type { ConfigListDto } from '@/api/emailConfig/types'
import type { AddType } from '@/api/types'
import { confirm } from '@/utils/util'
import { InsureTypeCnEnum } from '@/const/enum'

@Component
export class EmailRule extends TableRender {
    showDrawer = false

    showDrawerPreview = false

    curItem = {} as ConfigListDto

    type = 'add' as AddType

    tableColumnConfigOld = () =>
        [
            {
                field: 'operate',
                title: '操作',
                width: 60,
                params: {
                    buttons: [
                        {
                            text: '查看详情',
                            event: (row) => this.onHandlePreview(row)
                        },
                        {
                            text: '复制',
                            event: (row) => {
                                this.onCopy(row)
                            }
                        },
                        {
                            text: '编辑',
                            event: (row) => {
                                this.onEdit(row)
                            }
                        },
                        {
                            text: '删除',
                            content: '确定驳回么？',
                            type: 'danger',
                            event: (row) => {
                                this.onRemove(row)
                            }
                        }
                    ]
                },
                slots: {
                    default: 'tooltipButtons'
                }
            },
            {
                field: 'id',
                title: '规则ID'
            },
            {
                field: 'ruleName',
                title: '规则名称'
            },
            {
                field: 'insureType',
                title: '保险产品',
                formatter: ({row}) => {
                    return  InsureTypeCnEnum[Number(row.insureType)]
                }
            },
            {
                field: 'insureCompanyName',
                title: '关联保司',
            },
            {
                field: 'createUserName',
                title: '创建人'
            },
            {
                field: 'createTime',
                title: '创建时间',
                width: 160
            }
        ] as TableRenderColumnTypes<ConfigListDto>

    onHandlePreview(row: ConfigListDto) {
        this.$router.push({
            path: `/insurance-manage/rule-center/email-rule/${row.id}`
        })
    }

    onCopy(row: ConfigListDto) {
        this.type = 'newAdd'
        this.curItem = row
        this.showDrawer = true
    }

    onEdit(row: ConfigListDto) {
        this.type = 'edit'
        this.curItem = row
        this.showDrawer = true
    }

    async onRemove(row: ConfigListDto) {
        await confirm('确定删除吗?', '提示')
        await deleteConfig({
            id: row.id
        })
        message.success('删除成功')
        this.reloadPage()
    }

    onShowDrawer() {
        this.type = 'add'
        this.showDrawer = true
    }

    onLoadData(page: number, size: number, filters: AnyObject) {
        return getEmailConfigList({
            pageNum: page,
            pageSize: size,
            ...filters
        })
    }

    renderTitle() {
        return (
            <div class="flex items-center justify-between px-20px">
                <div>规则列表</div>
                <Button type="primary" icon={h(PlusOutlined)} onClick={this.onShowDrawer}>
                    新建保司规则
                </Button>
            </div>
        )
    }
    render() {
        return (
            <div class="h-full">
                {super.renderNoFilterTable()}
                {this.showDrawer && (
                    <DrawerAdd v-model:show={this.showDrawer} type={this.type} itemData={this.curItem} onRefresh={this.reloadPage} />
                )}
                {/* <DrawerPreview
                    v-model:show={this.showDrawerPreview}
                    id={this.curItem.id as number}
                /> */}
            </div>
        )
    }
}

export default toNative<{}, {}>(EmailRule)
